<?php
	class Product_model extends CI_Model {
	    
    var $id;
	var $code;    
	var $title;
	var $created_at;
	var $active;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    // load all Storage
    function loadAll()
    {
        $query = $this->db->get('products');
        return $query->result();
    }    

    // paging load
    function loadAllV2($start = false, $limit = false)
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->order_by('id','desc');
        $this->db->limit($limit,$start);

        $query = $this->db->get();

        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->countResult($start,$limit),
            );
        
        return $return;
    }

    // paging load
    function loadAllV3($start = false, $limit = false, $search = false)
    {
        $this->db->select('*');
        $this->db->from('products');

        if(!empty($search) && empty($search['category_id']) && empty($search['order']))
        {
            $where = "code = '".$search['code']."' OR slug LIKE '%".$search['slug']."%'";
            $this->db->where($where);
        }
        if(!empty($search['category_id']))
        {
            $this->db->where('category_id',$search['category_id']);
        }
        if(!empty($search['order']))
        {
            if($search['order'] == 'newest')
            {
                $this->db->order_by('id','desc');
            }
            else if($search['order'] == 'oldest')
            {
                $this->db->order_by('id','asc');   
            }
            else if($search['order'] == 'most-view')
            {
                $this->db->order_by('is_view','asc'); 
            }
        }

        if(empty($search['order']))
        {
            $this->db->order_by('id','desc');
        }

        $this->db->limit($limit,$start);

        $query = $this->db->get();           

        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->countResultV2($start,$limit,$search),
            );
        
        return $return;
    }

    function countResult($start = false, $limit = false)
    {
        $this->db->select('*');
        $this->db->from('products');
        return $this->db->count_all_results();
    }

    function countResultV2($start = false, $limit = false,$search = false)
    {
        $this->db->select('*');
        $this->db->from('products');
        if(!empty($search) && empty($search['category_id']) && empty($search['order']))
        {
            $where = "code = '".$search['code']."' OR slug LIKE '%".$search['slug']."%'";
            $this->db->where($where);
        }
        if(!empty($search['category_id']))
        {
            $this->db->where('category_id',$search['category_id']);
        }
        if(!empty($search['order']))
        {
            if($search['order'] == 'newest')
            {
                $this->db->order_by('id','desc');
            }
            else if($search['order'] == 'oldest')
            {
                $this->db->order_by('id','asc');   
            }
            else if($search['order'] == 'most-view')
            {
                $this->db->order_by('is_view','asc'); 
            }
        }
        return $this->db->count_all_results();
    }

    function getEntry($id)
    {
        $query = $this->db->get_where('products', array('id' => $id));
        return $query->row();
    }    

    function getInfo($code)
    {
        $query = $this->db->get_where('products', array('code' => $code));
        return $query->row();
    }

    // for homepage: show list of products
    function showListOfProducts($slug = false, $id = false)
    {
        $this->db->select('*');

        if(!empty($slug))
            $this->db->where('slug', $slug);
        if(!empty($id))
            $this->db->where('id', $id);

        $query = $this->db->get('products');

        if($query->num_rows() > 0)
        {
            if($query->num_rows() == 1)
            {
                return $query->row();
            }
            else
            {
                return $query->result();
            }
        }
        else
        {
            return false;
        }
    }

    // 
    function showListOfRelatedProducts($category_id = false, $product_id = false)
    {
        $this->db->select('*');
        if(!empty($category_id))
            $this->db->where('category_id', $category_id);
        $this->db->where('id !=',$product_id);
        $this->db->limit(4);

        $query = $this->db->get('products');
        return $query->result();
    }

    function insert($data)
    {
        $this->db->insert('products',$data);
    }

    function update($data, $id)
    {
        $this->db->where('id',$id);
        return $this->db->update('products',$data);
    }

    ///////////// special for testing /////////////////////////////
    function listProductsCart()
    {
        $this->db->select('*');        
        $this->db->limit(3);
        $this->db->order_by('id','desc');

        $query = $this->db->get('products');

        return $query->result();
    }

    function listProductWithSize($order_id = false, $product_id = false)
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->join('order_sizes','order_sizes.product_id = products.id');
        $this->db->where('order_sizes.order_id',$order_id);
        
        $query = $this->db->get();
        return $query->result();
    }

    function listProductSizeWithQuantity($order_id)
    {
        $this->db->select('*');
        $this->db->from('order_sizes');
        $this->db->join('product_sizes','product_sizes.product_id = order_sizes.product_id');
        $this->db->where('order_sizes.order_id',$order_id);

        $query = $this->db->get();
        return $query->result();
    }

    function loadAllProductDesigned($start = false, $limit = false, $search = false)
    {
        $this->db->select('*');
        $this->db->from('products');

        if(!empty($search))
        {            
            $this->db->where('user_id', $search);
        }

        $this->db->order_by('id','desc');
        //$this->db->limit($limit,$start);

        $query = $this->db->get();               

        $return = array(
                'data'  =>  $query->result(),
                // 'count' =>  $this->countResultV2($start,$limit,$search),
            );
        
        return $return;
    }

    ///////////////////////////////////////////////////////////////
}